package com.cgbl.cg.vm

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.util.ToastUtils
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.binding.command.BindingConsumer
import me.goldze.mvvmhabit.http.BaseResponse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ReportViewModel(application: Application) : GLBaseViewModel(application) {
    val imgList = ArrayList<String>()
    val typeList = arrayListOf<String>(
        "与社区文化不符", "广告营销", "广告传销",
        "诈骗", "色情", "暴力", "反动", "抄袭", "其它"
    )

    var select = "与社区文化不符"

    val name = ObservableField<String>()
    val nameEditSize = ObservableField<String>("0/100")
    val nameCommand = BindingCommand<String>(BindingConsumer {
        nameEditSize.set("${it.length}/100")
    })

    var id = ""
    fun doUpload(
        fileName: String,
        path: String,
        success: (uploadBean: String) -> Unit
    ) {
        val fileRQ: RequestBody = RequestBody.create(
            "multipart/form-data".toMediaTypeOrNull(),
            File(path)
        )
        val part = MultipartBody.Part.createFormData("file", fileName, fileRQ)
//        showLoadingDialog()
        UserServiceFactory.upload(part)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
//                    dismissLoadingDialog()
                    ToastUtils.showShort("上传成功")
                    success.invoke(t.data)
                    imgList.add(t.data)
                }
            })
    }
}